package com.xc.controller;

import com.xc.common.ServerResponse;
import com.xc.service.IAgentUserService;
import com.xc.utils.PropertiesUtil;
import com.xc.utils.redis.CookieUtils;
import com.xc.utils.redis.JsonUtil;
import com.xc.utils.redis.RedisConst;
import com.xc.utils.redis.RedisShardedPoolUtils;
import com.xc.vo.agent.AgentLoginResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author mipha
 * @version 1.0
 * @date 2019/2/24 3:32 PM
 */
@Controller
@RequestMapping("/api/agent/")
@Slf4j
public class AgentApiController {

    @Autowired
    IAgentUserService iAgentUserService;

    /**
     *  代理登陆
     * @param agentPhone
     * @param agentPwd
     * @param verifyCode
     * @param httpSession
     * @param httpServletRequest
     * @param httpServletResponse
     * @return
     */
    @RequestMapping("login.do")
    @ResponseBody
    public ServerResponse login(@RequestParam("agentPhone") String agentPhone,
                                @RequestParam("agentPwd") String agentPwd,
                                @RequestParam("verifyCode") String verifyCode,
                                HttpSession httpSession,
                                HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse){

        ServerResponse serverResponse = iAgentUserService
                .login(agentPhone,agentPwd,verifyCode,httpServletRequest);

        String agent_cookie_name = PropertiesUtil.getProperty("agent.cookie.name");
        String token = RedisConst.getAgentRedisKey(httpSession.getId());

        if(serverResponse.isSuccess()){

            CookieUtils.writeLoginToken(httpServletResponse
                    , token , agent_cookie_name);
            //  在redis服务器上保存用户的登陆信息
            String redisSetExResult = RedisShardedPoolUtils.setEx(token
                    , JsonUtil.obj2String(serverResponse.getData()),
                    RedisConst.RedisCacheExTime.REDIS_SESSION_EXTIME);

            log.info("redis setex agent result : {}",redisSetExResult);

            AgentLoginResultVO resultVO = new AgentLoginResultVO();
            resultVO.setKey(agent_cookie_name);
            resultVO.setToken(token);
            return ServerResponse.createBySuccess("登陆成功",resultVO);
        }else{
            return serverResponse;
        }
    }

    /**
     *  代理退出登录
     * @param httpServletRequest
     * @param httpServletResponse
     * @return
     */
    @RequestMapping("logout.do")
    @ResponseBody
    public ServerResponse logout(HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse){
        String cookie_name = PropertiesUtil.getProperty("agent.cookie.name");
        String logintoken = CookieUtils.readLoginToken(httpServletRequest,cookie_name);
        log.info("代理 token = {} ,退出登陆",logintoken);
        //  删除redis服务器上的logintoken
        RedisShardedPoolUtils.del(logintoken);
        //  删除cookie
        CookieUtils.delLoginToken(httpServletRequest,httpServletResponse,cookie_name);
        return ServerResponse.createBySuccess();
    }




}
